import axios from 'axios'
import request from 'sync-request'
import { loading } from './loading'
import { success, warning, error } from './message'
import { formDataInit, setUrlParam, downFileIframe, storageGet } from '@/utils'
import router from '@/router'
const baseURL = ''
const instance = axios.create({
    baseURL: window.Config.baseURL,
    timeout: 60000 * 5
})
let timer
instance.interceptors.request.use((config)=>{//请求拦截
    config.headers.accessToken = storageGet('token')
    return config
}, function(err){
    // 对请求错误做些什么
    error(err.message)
    return Promise.reject(err)
})
instance.interceptors.response.use((response) =>{//返回处理
    loading.close()
    if (response.status == 200 && response.data){
        if(response.data.status == '0'){
            error(response.data.msg)
        }
        return response.data
    }
    if (timer){
        clearTimeout(timer)
        timer = null
    }
    timer = setTimeout(()=>{
        if(response.data.msg == '认证失败，请重新认证！'){
            if(!localStorage.getItem('MODEDATA')){
                error(response.data.msg)
            }
            router.push({
                path: '/loginPage',
                query: {
                    // bacUrl: encodeURIComponent(location.href)
                }
            })
        }else{
            error(response.data.msg)
        }
    }, 200)

    return {}
}, function(err){
    // 对响应错误做点什么
    if (timer){
        clearTimeout(timer)
        timer = null
    }
    timer = setTimeout(()=>{
        error(err.message)
    }, 200)
    loading.close()
    return {}
})
export const postFormData = (param) => {
    if (param.loadingSHow !== false){
        loading.show()
    }
    return instance({
        url: param.url,
        method: 'post',
        data: formDataInit(param.data)
    })
}
export const post = (param) => {
    if (param.loadingSHow !== false){
        loading.show()
    }
    return instance({
        url: param.url,
        method: 'post',
        data: param.data
    })
}
export const postFlie = (param) => {
    if (param.loadingSHow !== false){
        loading.show()
    }
    return instance({
        url: param.url,
        method: 'post',
        data: formDataInit(param.data),
        headers: {
            'Content-type': 'multipart/form-data'
        }
    })
}
export const get = (param) => {
    if (param.loadingSHow !== false){
        loading.show()
    }
    return instance({
        url: setUrlParam(param.url, param.data),
        method: 'get',
        data: formDataInit(param.data)
    })
}
export const downFile = (param) => {
    // postFormData(param).then(res=>{
    // if(res.status == 0){
    // }else{
    downFileIframe(window.Config.baseURL + param.url, param.data, param.type)
    // }
    // })
}

export const syncRequest = (param)=>{
    const requestData = request(
        param.method || 'GET',
        window.Config.baseURL + setUrlParam(param.url, param.data),
        {
            headers: {
                accessToken: storageGet('token')
            }
        }
    )
    let data
    if (requestData.statusCode == 200 && requestData.body){
        const body = JSON.parse(requestData.body)
        if (body && body.status && body.status == 1){
            data = body.data
        } else {
            data = body
        }
    }
    if(requestData.body && requestData.body.indexOf('认证失败，请重新认证！') > -1){
        router.push({
            path: '/loginPage',
            query: {
                bacUrl: encodeURIComponent(location.href)
            }
        })
    }
    // if (requestData.statusCode == 203){
    //     loginUtils.toLogin()
    //     VM && VM.$warning('身份认证已过期,请重新登录')
    //     return {}
    // }
    return data
}

